package com.ysxsoft.wdg.ui.activity.login;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.socks.library.KLog;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.order.PinOrderDetailActivity;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.ui.activity.KanWebViewActivity;
import com.ysxsoft.wdg.ui.activity.MainActivity;
import com.ysxsoft.wdg.util.delayaction.Action;
import com.ysxsoft.wdg.util.delayaction.SingleCall;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.statusbar.StatusBarUtil;

import io.reactivex.functions.Consumer;

public class SplashActivity extends BaseActivity implements Action {


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0://登录
                    SingleCall.getInstance().addAction(SplashActivity.this)
                            .addValid(new LoginValid(mContext,0)).doCall();
                    break;
                case 1://主页
                    startActivity(new Intent(mContext, MainActivity.class));
                    break;
                default:
                    break;
            }
            finish();
        }
    };

    @Override
    protected int getLayoutId() {
        setTheme(R.style.AppTheme);
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {

//        String orderId, String money, int type, String url
        if(getIntent()!=null){
            Uri uri = getIntent().getData();
            if(uri!=null){
                String orderId= uri.getQueryParameter("did");
                String money= uri.getQueryParameter("money");
                String type= uri.getQueryParameter("type");
                String url= uri.getQueryParameter("url");

                Intent intent = new Intent(SplashActivity.this, PinOrderDetailActivity.class);
                intent.putExtra("orderid",orderId);
                intent.putExtra("money",money);
                intent.putExtra("type",type);//支付类型
                intent.putExtra("url",url);//支付成功后的url
                startActivity(intent);
                finish();
                return;
            }
        }

        StatusBarUtil.setTranslucentStatus(this, true);
        StatusBarUtil.setStatusBarTextColor(this, true);
        initLocation();

        boolean isFirst = SharePrefUtils.isStartFirst(this);
        if (isFirst){
            SharePrefUtils.saveStartStatus(false);
            handler.sendEmptyMessageDelayed(0,500);
        }else {
            handler.sendEmptyMessageDelayed(1,500);
        }
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void call() {
        MainActivity.start(this);
        finish();
    }


    //声明AMapLocationClientOption对象
    public AMapLocationClientOption mLocationOption = null;
    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //可在其中解析amapLocation获取相应内容。
                    SharePrefUtils.saveLatitude(amapLocation.getLatitude()+"");
                    SharePrefUtils.saveLongitude(amapLocation.getLongitude()+"");
                    SharePrefUtils.saveAddress(amapLocation.getCity().replace("市",""));
                    KLog.e("Latitude="+amapLocation.getLatitude()+",Longitude="+amapLocation.getLongitude());
                }else {
                    //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                    KLog.e("AmapError","location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };
    private void initLocation() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);


        new RxPermissions(this)
                .request(Manifest.permission.ACCESS_FINE_LOCATION)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) {
                        //启动定位
                        mLocationClient.startLocation();
                    }
                });
    }
}
